package com.getpebble.android.framework.datalogging;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.pebblekit.PebbleKit;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage.ByteDataItem;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage.DataItem;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage.Datatype;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage.SignedIntDataItem;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage.UnsignedIntDataItem;
import com.getpebble.android.framework.util.ByteUtils;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.nio.ByteBuffer;

public class DataloggingItem {
    private static final String TAG = DataloggingItem.class.getSimpleName();
    private final int mDataId;
    private final DataItem mDataObject;
    private final DataloggingDb mDatabase;
    private final PebbleKit mPebbleKit;
    private final DataloggingSession mSession;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundDataloggingMessage$Datatype = new int[Datatype.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundDataloggingMessage$Datatype[Datatype.BYTE_ARRAY.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundDataloggingMessage$Datatype[Datatype.UNSIGNED_INTEGER.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundDataloggingMessage$Datatype[Datatype.SIGNED_INTEGER.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    DataloggingItem(DataloggingSession session, DataItem item, DataloggingDb database, PebbleKit pebbleKit, int dataId) throws SQLException {
        if (database == null) {
            throw new IllegalArgumentException("database cannot be null");
        } else if (session == null) {
            throw new IllegalArgumentException("session cannot be null");
        } else if (item == null) {
            throw new IllegalArgumentException("item cannot be null");
        } else if (pebbleKit == null) {
            throw new IllegalArgumentException("pebbleKit cannot be null");
        } else {
            this.mSession = session;
            this.mDataId = dataId;
            this.mDataObject = item;
            this.mDatabase = database;
            this.mPebbleKit = pebbleKit;
            Trace.verbose(TAG, "DataloggingItem() from message: session = " + this.mSession.getSessionId() + " dataId = " + this.mDataId + " size = " + session.getItemSize());
            this.mDatabase.insertItemRecord(this);
        }
    }

    private static UnsignedInteger getUnsignedIntegerFromBytes(byte[] bytes, DataloggingSession session) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        switch (session.getItemSize().intValue()) {
            case SMTPMessage.RETURN_FULL /*1*/:
                return ByteUtils.getUint8FromBuffer(buffer);
            case SMTPMessage.RETURN_HDRS /*2*/:
                return ByteUtils.getUint16FromBuffer(buffer);
            default:
                return ByteUtils.getUint32FromBuffer(buffer);
        }
    }

    private static int getIntFromBytes(byte[] bytes, DataloggingSession session) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        switch (session.getItemSize().intValue()) {
            case SMTPMessage.RETURN_FULL /*1*/:
                return buffer.get();
            case SMTPMessage.RETURN_HDRS /*2*/:
                return buffer.getShort();
            default:
                return buffer.getInt();
        }
    }

    DataloggingItem(Cursor cursor, DataloggingSession session, DataloggingDb database, PebbleKit pebbleKit) {
        if (cursor == null) {
            throw new IllegalArgumentException("cursor cannot be null");
        } else if (session == null) {
            throw new IllegalArgumentException("session cannot be null");
        } else if (database == null) {
            throw new IllegalArgumentException("database cannot be null");
        } else if (pebbleKit == null) {
            throw new IllegalArgumentException("pebbleKit cannot be null");
        } else {
            this.mSession = session;
            this.mDatabase = database;
            this.mPebbleKit = pebbleKit;
            this.mDataId = cursor.getInt(cursor.getColumnIndex("data_id"));
            byte[] data = cursor.getBlob(cursor.getColumnIndex("data_object"));
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundDataloggingMessage$Datatype[session.getDataType().ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    this.mDataObject = new ByteDataItem(data);
                    return;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    this.mDataObject = new UnsignedIntDataItem(getUnsignedIntegerFromBytes(data, this.mSession), this.mSession.getItemSize());
                    return;
                case ListInfo.INDETERMINATE /*3*/:
                    this.mDataObject = new SignedIntDataItem(getIntFromBytes(data, this.mSession), this.mSession.getItemSize());
                    return;
                default:
                    Trace.warning(TAG, "unknown datatype: " + session.getDataType());
                    this.mDataObject = null;
                    return;
            }
        }
    }

    void ack() {
        this.mDatabase.deleteItemRecord(this);
    }

    void broadcast() {
        Trace.verbose(TAG, "broadcast data item: " + this.mDataId);
        this.mPebbleKit.broadcastDataloggingItem(this);
    }

    public DataloggingSession getSession() {
        return this.mSession;
    }

    public int getDataId() {
        return this.mDataId;
    }

    public DataItem getDataObject() {
        return this.mDataObject;
    }

    ContentValues getContentValues() {
        ContentValues values = new ContentValues();
        values.put("local_session_uuid", this.mSession.getLocalSessionUuid().toString());
        values.put("data_id", Integer.valueOf(this.mDataId));
        values.put("data_object", this.mDataObject.getByteValue());
        return values;
    }
}
